//无论什么变换先清空
function changesInfo() {
    $("#zzdz_addWinTable input[qucln=AUTHOR]").val();
    $("#zzdz_addWinTable input[qucln=AUTHOR]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=AUTHOR]").next().val('');//作者

    $("#zzdz_addWinTable input[qucln=BASIS_ACCOUNT]").val();
    $("#zzdz_addWinTable input[qucln=BASIS_ACCOUNT]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=BASIS_ACCOUNT]").next().val('');//结算依据

    $("#zzdz_addWinTable input[qucln=TYPE_TICKET]").val();
    $("#zzdz_addWinTable input[qucln=TYPE_TICKET]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=TYPE_TICKET]").next().val('');//发票类型

    $("#zzdz_addWinTable input[qucln=ROYALTY_SALE]").val();
    $("#zzdz_addWinTable input[qucln=ROYALTY_SALE]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=ROYALTY_SALE]").next().val('');//是否促销

    $("#zzdz_addWinTable input[qucln=PRICE]").val();
    $("#zzdz_addWinTable input[qucln=PRICE]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=PRICE]").next().val('');//定价

    $("#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]").val();
    $("#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]").next().val('');//版税参数

    $("#zzdz_addWinTable input[qucln=TAX_RATE]").val();
    $("#zzdz_addWinTable input[qucln=TAX_RATE]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=TAX_RATE]").next().val(''); //税率

    $("#zzdz_addWinTable input[qucln=DJ_TYPE]").val();
    $("#zzdz_addWinTable input[qucln=DJ_TYPE]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=DJ_TYPE]").next().val('');//版税类型

    $("#zzdz_addWinTable input[qucln=BALANCE_PREPAY]").val();
    $("#zzdz_addWinTable input[qucln=BALANCE_PREPAY]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=BALANCE_PREPAY]").next().val('');//预付余额

    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").val();
    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").next().val('');//本期印数

    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").val();
    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").next().val('');//本期净发货数

    $("#zzdz_addWinTable input[qucln=NUMBER_SALE]").val();
    $("#zzdz_addWinTable input[qucln=NUMBER_SALE]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_SALE]").next().val('');//促销数

    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]").val();
    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]").next().val('');//历史总净发货数

    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]").val();
    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]").next().val('');//历史总印数

    $("#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]").val();
    $("#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]").attr("chan", "T");
    $("#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]").next().val('');//计算公式

    $("#zzdz_addWinTable input[qucln=PAY_SHOULD]").val();
    $("#zzdz_addWinTable input[qucln=PAY_SHOULD]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=PAY_SHOULD]").next().val('');//应付

    $("#zzdz_addWinTable input[qucln=PAY_REAL]").val();
    $("#zzdz_addWinTable input[qucln=PAY_REAL]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=PAY_REAL]").next().val('');//实付

    $("#zzdz_addWinTable input[qucln=MEMO]").val();
    $("#zzdz_addWinTable input[qucln=MEMO]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=MEMO]").next().val('');//备注

    $("#zzdz_addWinTable input[qucln=DJ_CODE]").val();
    $("#zzdz_addWinTable input[qucln=DJ_CODE]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=DJ_CODE]").next().val('');//单据号

    $('#zzdz_addItemDatagrid').datagrid('loadData', { total: 0, rows: [] });
}


//当重新选择付款对象时，清空所有信息
function getClearPayInfo() {
    resertXtInfo();
    changesInfo();
}
////当选题名称在改变时，要清空的东西
function getXtInfoClear() {
    changesInfo();
}

function ZZDZ_autoCheck(_this) {

    // 获取选题名称
    var XT_NAME = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    // 获取付款对象
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    if(!XT_NAME) {
        return;
    }
    $('#zzdz_addItemDatagrid').datagrid('loadData', { total: 0, rows: [] });
    $('#zzdz_addItemDatagrid').datagrid('loading');
    $.ajax({
        type: 'post',
        url: _m_url + 'bjTopic/loadDZInfos',
        data: {'dsId':'ds_zc_authorcheck_item2','XT_NAME': XT_NAME,'PAY_PARTY': PAY_PARTY},
        success: function (data) {
            $('#zzdz_addItemDatagrid').datagrid('loaded');
            $.each(data.rows, function (i, v) {
                $('#zzdz_addItemDatagrid').datagrid('appendRow', v);
            });
            if(data.tipStr) {
                $.messager.alert('提示', data.tipStr);
            }
            var total = 0;
            $.each(data.rows, function (i, v) {
                total += Number(v.AMOUNT);
            });
            $("#zzdz_congfigValItem").html('数量合计：'+total);

            zzdz_sure1();
        }
    });

    // $('#zzdz_addItemDatagrid').datagrid({
    //     url:_m_url + 'bjTopic/loadDZInfos',
    //     queryParams:{'dsId':'ds_zc_authorcheck_item2','XT_NAME': XT_NAME,'PAY_PARTY': PAY_PARTY},
    //     onLoadSuccess:function(data){
    //         if(total && total == 0) {
    //             return;
    //         }
    //         $.each(data.rows, function (i, v) {
    //             $('#zzdz_addItemDatagrid').datagrid('appendRow', v);
    //         });
    //         if(data.tipStr) {
    //             $.messager.alert('提示', data.tipStr);
    //         }
    //         var total = 0;
    //         $.each(data.rows, function (i, v) {
    //             total += Number(v.AMOUNT);
    //         });
    //         $("#zzdz_congfigValItem").html('数量合计：'+total);
    //     }
    // });
}

//查询界面
function autoCheckAccount() {
    $('#autoCheckAccountDialog').show();
    $('#autoCheckAccountDialog').dialog({
        width:700,
        height:480,
        // maximized:true,
        // collapsible: true,
        // maximizable: true,
        // minimizable: true,
        title:'自动对账',
        left:150,
        top:30,
        modal: true,
        buttons:[{ //设置下方按钮数组
            id: 'execute',
            text:'执行',
            iconCls:'icon-ok',
            disabled: true,
            handler:function(){
                $(this).linkbutton('disable');
                var span = $(this).find('.icon-ok');
                span.removeClass('icon-ok');
                span.addClass('icon-loading');
                span.html('执行中。。。');
                executeAutoCheckAccount($(this));
            }
        },{
            text:'关闭',
            iconCls:'icon-cancel',
            handler:function(){
                $('#autoCheckAccountDialog').dialog('close');
                $('#zzdz_addDialogForm input').val('');

                if(refreshData) {
                    $('#zzdz_QUERY_BUT').click();
                }
            }
        }]
    });
}

var refreshData = false;

/**
 * 执行自动对账
 */
function executeAutoCheckAccount(btn) {
    var PAY_PARTY = $('#zzdz_addDialogForm').find('input[qucln=PAY_PARTY]').val();
    var XT_NAME = $('#zzdz_addDialogForm').find('input[qucln=TOPIC_SELECTED]').val();
    var sfct = $('#zzdz_addDialogForm').find('select[qucln=sfct]').val();
    if(!PAY_PARTY) {
        $.messager.alert('提示', '请选择付款对象！');
        return;
    }

    if(!XT_NAME) {
        $.messager.alert('提示', '请选择选题！');
        return;
    }

    $.ajax({
        type: 'post',
        url: _m_url + 'bjTopic/executeAutoCheckAccount',
        data: {
            'dsId': 'ds_zc_authorcheck1;ds_zc_authorcheck_item2',
            'PAY_PARTY': PAY_PARTY,
            'XT_NAME': XT_NAME,
            'sfct': sfct
        },
        dataType: 'json',
        success: function (data) {
            if(data.success) {
                $.messager.alert('提示', data.msg);
                refreshData = true;
            } else {
                $.messager.alert('提示', data.msg);
            }

            var span = btn.find('.icon-loading');
            span.removeClass('icon-loading');
            span.addClass('icon-ok');
            span.html('已完成');
        }
    });

}

//清除选题名称
function resertXtInfo() {
    $('#zzdz_addWinTable input[qucln=TOPIC_SELECTED]').val('');
    $('#zzdz_addWinTable input[qucln=TOPIC_SELECTED]').attr("chan", "T");
    $('#zzdz_addWinTable input[qucln=TOPIC_SELECTED]').next().val('');//选题名称
}

function checkBasisAccount() {
    var mx1 = $('#zzdz_addWinTable').find('input[qucln=BASIS_ACCOUNT]').val();
    if (mx1 == '净发货数' || mx1 == 'dz_bsjsyj.003') {
        $('#zzdz_autoyw').linkbutton("enable");
        $('#zzdz_addyw').linkbutton("disable");
    } else {
        $('#zzdz_autoyw').linkbutton("disable");
        $('#zzdz_addyw').linkbutton("enable");
    }
}

//根据选题名称拉取选题合同信息
function getzzXTAccount() {

    // 获取选题名称
    var XT_NAME = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    // 获取付款对象
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    //选题名称的change事件
    $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').change(function () {
        //如果选题名称改变了，那么要把上一个选题的信息全部清空
        getXtInfoClear();
        checkBasisAccount();
    });

    $(PAY_PARTY).change(function () {
        //如果选题名称改变了，那么要把上一个选题的信息全部清空
        getClearPayInfo();
    });
    xt_change_check(PAY_PARTY, XT_NAME);
    $.ajax({
        type: 'post',
        url: _m_url + 'bjTopic/getInfobyParty',
        data: {
            'XT_NAME': XT_NAME,
            'PAY_PARTY': PAY_PARTY
        },
        success: function (data) {
            //如果选题名称拉取的时候选题合同信息不止一条数据那么就拉取过来进行手动选择
            $("#zzdz_addWinTable input[qucln=AUTHOR]").val(data[0].AUTHOR);//存进数据库
            $("#zzdz_addWinTable input[qucln=AUTHOR]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=AUTHOR]").next().val(data[0].AUTHORFY);//页面显示 :作者

            $("#zzdz_addWinTable input[qucln=BASIS_ACCOUNT]").val(data[0].BASIS_ACCOUNT);
            $("#zzdz_addWinTable input[qucln=BASIS_ACCOUNT]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=BASIS_ACCOUNT]").next().val(data[0].BASIS_ACCOUNTFY);// 页面显示： 结算依据

            $("#zzdz_addWinTable input[qucln=TYPE_TICKET]").val(data[0].TYPE_TICKET);
            $("#zzdz_addWinTable input[qucln=TYPE_TICKET]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=TYPE_TICKET]").next().val(data[0].TYPE_TICKETFY);//页面显示：发票类型

            $("#zzdz_addWinTable input[qucln=ROYALTY_SALE]").val(data[0].ROYALTY_SALE);
            $("#zzdz_addWinTable input[qucln=ROYALTY_SALE]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=ROYALTY_SALE]").next().val(data[0].ROYALTY_SALEFY);//页面显示：是否促销

            $("#zzdz_addWinTable input[qucln=PRICE]").val(data[0].PRICE);
            $("#zzdz_addWinTable input[qucln=PRICE]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=PRICE]").next().val(data[0].PRICE);//定价

            $("#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]").val(data[0].PARAMETER_ROYALTY);
            $("#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]").next().val(data[0].PARAMETER_ROYALTY);//版税参数

            $("#zzdz_addWinTable input[qucln=TAX_RATE]").val(data[0].TAX_RATE);
            $("#zzdz_addWinTable input[qucln=TAX_RATE]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=TAX_RATE]").next().val(data[0].TAX_RATE);//税率

            $("#zzdz_addWinTable input[qucln=DJ_TYPE]").val(data[0].DJ_TYPE);
            $("#zzdz_addWinTable input[qucln=DJ_TYPE]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=DJ_TYPE]").next().val(data[0].DJ_TYPE);//版税类型

            $("#zzdz_addWinTable input[qucln=BALANCE_PREPAY]").val(data[0].BALANCE_PREPAY);
            $("#zzdz_addWinTable input[qucln=BALANCE_PREPAY]").attr("chan", "T");
            $("#zzdz_addWinTable input[qucln=BALANCE_PREPAY]").next().val(data[0].BALANCE_PREPAY);//预付余额

            checkBasisAccount();
        }
    });
}

function sureSelectXtName() {
    var XT_NAME = $('#zzdz_addDialogForm').find('input[qucln=TOPIC_SELECTED]').val();
    // 获取付款对象
    var PAY_PARTY = $('#zzdz_addDialogForm').find('input[qucln=PAY_PARTY]').val();
    //如果选题名称改变了，那么要把上一个选题的信息全部清空
    xt_change_check(PAY_PARTY, XT_NAME);
}

/**
 * 选题变更时检查该付款对象和选题是否存在未审核单据
 */
function xt_change_check(PAY_PARTY, XT_NAME) {
    $.ajax({
        type: 'post',
        url: _m_url + 'bjTopic/checkExistsNotVerifyDj',
        data: {
            'XT_NAME': XT_NAME,
            'PAY_PARTY': PAY_PARTY
        },
        success: function (data) {
            if(data == 'true' || data) {
                $.messager.alert('提示', '该付款对象和选题存在保存未审核单据，请先审核该单据。');
                $('#execute').linkbutton('disable');
            } else {
                $('#execute').linkbutton('enable');
            }
            var span = $('#execute').find('.l-btn-text');
            span.html('执行');
        }
    });
}

////选题合同改变时明细清空
//$("#zzdz_A_ADATAL input[qucln='TOPIC_SELECTED']").change(function () {
//    $('#zzdz_addItemDatagrid').datagrid('loadData', {total: 0, rows: []});
//});
//$("#zzdz_A_ADATAL input[qucln='PAY_PARTY']").change(function () {
//    $('#zzdz_addItemDatagrid').datagrid('loadData', {total: 0, rows: []});
//});

//选择单据的弹框加载页面
function ZZDZ_codeSelected() {
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    var TOPIC_SELECTED = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    var mx1 = $('#zzdz_addWinTable').find('input[qucln=BASIS_ACCOUNT]').val()

    if (mx1 == '净发货数' || mx1 == 'dz_bsjsyj.003') {
        _data_qw('zzdjxz', 'ds_zc_authorcheck_item2', 'zzdz_sure1', '44', 'zzdz_clowQuery', '');
    }
    else if (mx1 == 'dz_bsjsyj.001' || mx1 == '印数') {
        _data_qw('zzdzyz', 'ds_zc_authorcheck_item3', 'zzdz_sure2', '44', 'zzdzyz_clowQuery', '');
    }
    else if (PAY_PARTY == '' || PAY_PARTY == null) {
        $.messager.alert('提示', '请选择付款对象')
        return;
    }

    else if (TOPIC_SELECTED == '' || TOPIC_SELECTED == null) {
        $.messager.alert('提示', '请选择选题名称')
        return;
    }
}

//删除选择的单据
function ZZDZ_codeSelectedDel() {
    var selRows = $('#zzdz_addItemDatagrid').datagrid('getSelections');
    $.each(selRows, function (i, v) {
        var index = $('#zzdz_addItemDatagrid').datagrid('getRowIndex', v);
        $('#zzdz_addItemDatagrid').datagrid('deleteRow',index);
    });
}

//按净发货数弹出窗口的回调函数
function zzdz_clowQuery() {
    $('#zzdjxz_dataQuery').attr('onclick', 'zzdz_clcjsQuery(this)');
}

//按印数弹出窗口的回调函数
function zzdzyz_clowQuery() {
    $('#zzdzyz_dataQuery').attr('onclick', 'zzdzyz_clcjsQuery(this)');
}

//结算清单的按净发货数查询按钮
function zzdz_clcjsQuery(obj) {
    var prefix = $(obj).attr('pre');
    var dsId = $(obj).attr('dsid');
    var resultDG = 'DATA_DATALIST';
    var comUseConf = 'commUseD';
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    var TOPIC_SELECTED = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    var oneConfig = "1-TOPIC_SELECTED-" + TOPIC_SELECTED + "-inc|+";//查询条件拼接（选题）
    var twoConfig = "1-PAY_PARTY-" + PAY_PARTY + "-inc|+";//查询条件拼接（付款对象）
    var comUse = parseConfig(prefix + "_" + comUseConf);
    var useConf = oneConfig + twoConfig + comUse;
    var ids = '';
    var queryDG = $('#' + prefix + "_" + resultDG);
    queryDG.datagrid({
        pageNumber: 1,
        pagination: true,
        pageList: [300],
        url: _m_url + 'framework/loadDataQ',
        queryParams: {'dsId': dsId, 'config': useConf, "originalSql": ids}
    });
}


//结算清单的按印数的查询按钮
function zzdzyz_clcjsQuery(obj) {
    var prefix = $(obj).attr('pre');
    var dsId = $(obj).attr('dsid');
    var resultDG = 'DATA_DATALIST';
    var comUseConf = 'commUseD';
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    var TOPIC_SELECTED = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    var oneConfig = "1-TOPIC_SELECTED-" + TOPIC_SELECTED + "-inc|+";//查询条件拼接（选题）
    var twoConfig = "1-PAY_PARTY-" + PAY_PARTY + "-inc|+";//查询条件拼接（付款对象）
    var comUse = parseConfig(prefix + "_" + comUseConf);
    var useConf = oneConfig + twoConfig + comUse;

    //获取对账明细的数据行
    var dzmxRows = $('#zzdz_addItemDatagrid').datagrid('getRows');
    var ids = '';
    if (dzmxRows.length > 0) {
        for (var i = 0, j = dzmxRows.length; i < j; i++) {
            ids += "'" + dzmxRows[i].NUMBER_PRINT + "',";
        }
        ids = ids.substring(0, ids.lastIndexOf(","));
        ids = "  and NUMBER_PRINT not in (" + ids + ")"
    }

    var queryDG = $('#' + prefix + "_" + resultDG);
    queryDG.datagrid({
        pageNumber: 1,
        pagination: true,
        pageList: [300],
        url: _m_url + 'framework/loadDataQ',
        queryParams: {'dsId': dsId, 'config': useConf, "originalSql": ids}
    });
}


/**
 * 按净发货数时，弹窗的确定按钮
 *
 * @return
 */
function zzdz_sure1() {
    // 关闭弹窗
    // $('#zzdjxz_DATA_PARENT_PANEL').window('close');
    // 原有明细数据
    var oldRows = $('#zzdz_addItemDatagrid').datagrid('getRows');
    // 获取选择的数据
    // var selRows = $('#zzdjxz_DATA_DATALIST').datagrid('getSelections');
    //判断是否促销:1 是；0否
    var ROYALTY_SALE = $('#zzdz_addWinTable').find('input[qucln=ROYALTY_SALE]').val();
    //版税类型
    var TYPE = $('#zzdz_addWinTable').find('input[qucln=DJ_TYPE]').val();
    // 获取选题名称
    var XT_NAME = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    // 获取付款对象
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    // 获取定价
    var djPrice = $('#zzdz_addWinTable').find('input[qucln=PRICE]').val();
    //发票类型
    var TYPE_TICKET = $('#zzdz_addWinTable').find('input[qucln=TYPE_TICKET]').val();
    //结算依据
    var BASIS_ACCOUNT = $('#zzdz_addWinTable').find('input[qucln=BASIS_ACCOUNT]').val();
    var jfhCount = 0;//净发货数
    var cxCount = 0;//促销数
    var ys = 0//本期印数
    var hostys = 0//历史本期印数
    //历史总发货数
    amount();

    // for (var j = 0; j < selRows.length; j++) {
    //     var realAM = selRows[j].AMOUNT;
    //     //当促销单为否的时候 不能选择促销单的数据进行对账
    //     if (ROYALTY_SALE == 0 && TYPE == 'FX') {
    //         $.messager.alert('提示', '该选题不能使用促销单的单据进行对账，请重新选择对账单据！');
    //         continue;
    //     }
    //     var b = false;
    //     for (var i = 0; i < oldRows.length; i++) {
    //         if (oldRows[i].DJ_TYPE == selRows[j].DJ_TYPE && oldRows[i].ZZDZ_MONTH == selRows[j].ZZDZ_MONTH) {
    //             $.messager.alert('提示', '该单据已选择，不能重复对账！');
    //             b = true;
    //             break;
    //         }
    //     }
    //     if (b) {
    //         continue;
    //     }
    //     $('#zzdz_addItemDatagrid').datagrid('appendRow', {
    //         ZZDZ_MONTH: selRows[j].ZZDZ_MONTH,
    //         DJ_TYPE: selRows[j].DJ_TYPE,
    //         TOPIC_SELECTED: selRows[j].TOPIC_SELECTED,
    //         ZZDZ_ISBN: selRows[j].ZZDZ_ISBN,
    //         AMOUNT: realAM,
    //         NUMBER_PRINT: selRows[j].NUMBER_PRINT,
    //         BDUSE_MONEY: 0
    //     });
    // }
    var havRows = $('#zzdz_addItemDatagrid').datagrid('getRows');
    for (var k = 0; k < havRows.length; k++) {
        var tp = havRows[k].DJ_TYPE;
        var realAM = havRows[k].AMOUNT;
        if (ROYALTY_SALE == 1) {
            //促销单，退单，销售单
            if (tp == 'FX') {
                //促销单
                cxCount += realAM;
            } else if (tp == 'XS') {
                //销售单
                jfhCount += realAM;
            } else if (tp == 'XT') {
                //退单
                jfhCount += realAM;
            } else {
            }
        } else {
            //退单，销售单
            if (tp == 'XS') {
                //销售单
                jfhCount += realAM;
            } else if (tp == 'XT') {
                //退单
                jfhCount += realAM;
            } else {
                havRows[k].AMOUNT = 0;
                $('#zzdz_addItemDatagrid').datagrid('updateRow',{
                    index: k,
                    row: havRows[k]
                });
                cxCount = 0;
            }
        }
    }

    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").val(jfhCount);
    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").next().val(jfhCount); //本期净发货数数量

    $("#zzdz_addWinTable input[qucln=NUMBER_SALE]").val(cxCount);
    $("#zzdz_addWinTable input[qucln=NUMBER_SALE]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_SALE]").next().val(cxCount);//促销数数量

    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").val(ys);
    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").next().val(ys);//本期印数

    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]").val(hostys);
    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]").next().val(hostys);//历史印数

}


/**
 * 按印数，弹窗的确定按钮
 *
 * @return
 */
function zzdz_sure2() {
    // 关闭弹窗
    $('#zzdzyz_DATA_PARENT_PANEL').window('close');
    // 获取选择的数据
    var selRows = $('#zzdzyz_DATA_DATALIST').datagrid('getSelections');
    //版税类型
    var TYPE = $('#zzdz_addWinTable').find('input[qucln=DJ_TYPE]').val();
    //历史总印数
    amount();
    var jfhCount = 0;//本期印数
    var js = 0;//本期净发货数
    var hostjs = 0;//历史净发货数
    for (var j = 0; j < selRows.length; j++) {
        $('#zzdz_addItemDatagrid').datagrid('appendRow', {
            ZZDZ_MONTH: selRows[j].ZZDZ_MONTH,
            DJ_TYPE: selRows[j].DJ_TYPE,
            TOPIC_SELECTED: selRows[j].TOPIC_SELECTED,
            ZZDZ_ISBN: selRows[j].ZZDZ_ISBN,
            AMOUNT: selRows[j].AMOUNT,
            NUMBER_PRINT: selRows[j].NUMBER_PRINT,
            BDUSE_MONEY: 0
        });
    }
    var oldRows = $('#zzdz_addItemDatagrid').datagrid('getRows');
    for (var k = 0; k < oldRows.length; k++) {
        jfhCount += oldRows[k].AMOUNT;
    }
    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").val(jfhCount);
    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_PRINT]").next().val(jfhCount);//数量

    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").val(js);
    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_DELIVERY]").next().val(js);//数量

    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]").val(hostjs);
    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]").attr("chan", "T");
    $("#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]").next().val(hostjs);//数量

}
//历史总发货数,根据选题名称，付款对象，结算依据来判断 历史总发货数和历史的总印数
function amount() {
    // 获取选题名称
    var XT_NAME = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    // 获取付款对象
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    // 获取结算依据
    var jiesuan = $('#zzdz_addWinTable').find('input[qucln=BASIS_ACCOUNT]').val();
    $.ajax({
        type: 'post',
        url: _m_url + 'bjTopic/getamount',
        data: {'PAY_PARTY': PAY_PARTY, 'BASIS_ACCOUNT': jiesuan, 'TOPIC_SELECTED': XT_NAME},
        success: function (data) {
            if (data && data.length > 0) {
                //净发货数历史总和
                if (jiesuan == 'dz_bsjsyj.003') {
                    $('#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]').val(data[0].JNUMBER);
                    $('#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]').attr("chan", "T");
                    $('#zzdz_addWinTable input[qucln=NUMBER_TOTAL_DELIVERY]').next().val(data[0].JNUMBER);
                }//历史印数总和
                else if (jiesuan == 'dz_bsjsyj.001') {
                    $('#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]').val(data[0].YNUMBER);
                    $('#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]').attr("chan", "T");
                    $('#zzdz_addWinTable input[qucln=NUMBER_TOTAL_PRINT]').next().val(data[0].YNUMBER);
                }
            }
        }
    })
}


/***
 * 作者对账保存按钮方法
 * @param obj
 * @return
 */
function zzSave(obj) {
    //先判断是否有保存的单据
    var isexist=is_existdzdata();
    if (isexist==1) {
        return;
    }
    // 获取选题名称
    var XT_NAME = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    // 获取付款对象
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    // 获取定价
    var djPrice = $('#zzdz_addWinTable').find('input[qucln=PRICE]').val();
    //发票类型
    var TYPE_TICKET = $('#zzdz_addWinTable').find('input[qucln=TYPE_TICKET]').val();
    //结算依据
    var BASIS_ACCOUNT = $('#zzdz_addWinTable').find('input[qucln=BASIS_ACCOUNT]').val();
    //判断是否促销:1 是；0否
    var ROYALTY_SALE = $('#zzdz_addWinTable').find('input[qucln=ROYALTY_SALE]').val();
    // 历史总净发货数
    var NUMBER_TOTAL_DELIVERY = $('#zzdz_addWinTable').find('input[qucln=NUMBER_TOTAL_DELIVERY]').val();
    //历史总印数
    var NUMBER_TOTAL_PRINT = $('#zzdz_addWinTable').find('input[qucln=NUMBER_TOTAL_PRINT]').val();
    //本期净发货数
    var NUMBER_DELIVERY = $('#zzdz_addWinTable').find('input[qucln=NUMBER_DELIVERY]').val();
    //本期印数
    var NUMBER_PRINT = $('#zzdz_addWinTable').find('input[qucln=NUMBER_PRINT]').val();
    //促销数
    var NUMBER_SALE = $('#zzdz_addWinTable').find('input[qucln=NUMBER_SALE]').val();
    //版税类型
    var DJ_TYPE = $('#zzdz_addWinTable').find('input[qucln=DJ_TYPE]').val();
    //对账日期
    var DZ_DATE = $('#zzdz_addWinTable').find('input[qucln=DZ_DATE]').val();
    //分解对账日期年、月、日
    var inDate = new Date(
        DZ_DATE.split('-')[0],
        DZ_DATE.split('-')[1] - 1,
        DZ_DATE.split("-")[2]
    ).toLocaleDateString().substr(0, 7);

    //当前时间
    var date = new Date;
    var yearNow = date.toLocaleDateString().substr(0, 7);
    if (inDate == "Invalid") {
        $.messager.alert("提示信息", "对账月份不能为空!");
        return;
    }
    // else if (inDate == yearNow) {
    //
    //     $.messager.alert("提示信息", "对账月份不能等于当前月份!");
    //     return;
    // } else if (inDate > yearNow) {
    //
    //     $.messager.alert("提示信息", "对账月份不能大于当前月份!");
    //     return;
    // }
    if (new Date(DZ_DATE) > date) {
        $.messager.alert("提示信息", "对账日期不能大于当前日期!");
        return;
    }
    //印数
    if (BASIS_ACCOUNT == 'dz_bsjsyj.001') {
        ///固定版税率
        if (DJ_TYPE == 'contract.001') {
            //获取版税类型
            var banshui = 0;
            $.ajax({
                type: 'post',
                url: _m_url + 'bjTopic/getZZBSMoney',
                data: {'xtId': XT_NAME, 'manID': PAY_PARTY},
                async: false,
                success: function (data) {
                    if (data.length > 0) {
                        $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').val(data[0].PARAMETER_ROYALTY);
                        $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').attr("chan", "T");
                        $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').next().val(data[0].PARAMETER_ROYALTY);
                        banshui = data[0].PARAMETER_ROYALTY;//获取版税参数
                        var bsTrue = parseFloat(banshui); //版税的确认
                        var yinshuTrue = parseFloat(NUMBER_PRINT);//本期的印数的确认
                        var priceTrue = Math.round(parseFloat(djPrice) * 100) / 100;//定价的确认
                        var money = bsTrue * yinshuTrue * priceTrue;//应付价格
                        var reallyMoney = ((money * 88.8) / 100); //实付
                        if (TYPE_TICKET == 'dz_fptype.003') {
                            $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(reallyMoney * 100) / 100);
                            $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                            $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(reallyMoney * 100) / 100);
                        } else {
                            $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(money * 100) / 100);
                            $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                            $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(money * 100) / 100);
                        }
                        $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').val(Math.round(money * 100) / 100);
                        $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').attr("chan", "T");
                        $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').next().val(Math.round(money * 100) / 100);
                    }

                    var str = "计算公式：" + yinshuTrue + " * " + priceTrue + " * " + bsTrue + " = " + Math.round(money * 100) / 100;
                    //计算方式
                    $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').val(str);
                    $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').attr("chan", "T");
                    $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').next().val(str);
                }
            });
        } //阶梯版税率
        else if (DJ_TYPE == 'contract.002') {
            $.ajax({
                type: 'post',
                url: _m_url + 'bjTopic/getZZBSMoney',
                data: {'xtId': XT_NAME, 'manID': PAY_PARTY},
                async: false,
                success: function (data) {
                    $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').val(data[0].PARAMETER_ROYALTY);
                    $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').attr("chan", "T");
                    $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').next().val(data[0].PARAMETER_ROYALTY);
                    var runIds = new Array();
                    //获取入库数
                    var rkMount = NUMBER_PRINT;
                    //获取版税临时表中的dsid
                    var dsId = 'ds_zc_authorcheck1';
                    var sinid = data[0].SIN_ID;
                    var price = djPrice;
                    runIds.push(rkMount);
                    runIds.push(dsId);
                    runIds.push(sinid);
                    runIds.push(PAY_PARTY);
                    runIds.push(XT_NAME);
                    runIds.push(price);
                    runIdtwo = runIds;
                    console.log(runIds);
                    $.ajax({
                        type: 'post',
                        url: _m_url + 'bjTopic/getzzJTBM',
                        contentType: "application/json;charset=utf-8",
                        data: JSON.stringify(runIds),
                        async: false,
                        success: function (data) {
                            var str = data[0];
                            var bsmoney = str.substring(str.indexOf("B") + 1, str.indexOf("A"));

                            var reallyMoney = ((bsmoney * 88.8) / 100); //实付
                            if (TYPE_TICKET == 'dz_fptype.003') {
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(reallyMoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(reallyMoney * 100) / 100);
                            } else {
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(bsmoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(bsmoney * 100) / 100);
                            }
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').val(Math.round(bsmoney * 100) / 100);
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').attr("chan", "T");
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').next().val(Math.round(bsmoney * 100) / 100);

                            //计算方式
                            $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').val(str.substring(0, str.indexOf("B")));
                            $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').attr("chan", "T");
                            $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').next().val(str.substring(0, str.indexOf("B")));
                        }
                    })

                }
            });


        }//固定金额
        else if (DJ_TYPE == 'contract.003') {
            $.messager.alert('提示', '版税类型为“固定金额”，不进行对账,请重新选择对账的选题！');
            return;
        }//按千字
        else if (DJ_TYPE == 'contract.008') {
            $.messager.alert('提示', '版税类型为“按千字”，不进行对账,请重新选择对账的选题！');
            return;
        }
    }
    //净发货数
    else {
        //固定版税率
        if (DJ_TYPE == 'contract.001') {
            //获取版税类型
            var banshui = 0;
            $.ajax({
                type: 'post',
                url: _m_url + 'bjTopic/getZZBSMoney',
                data: {'xtId': XT_NAME, 'manID': PAY_PARTY},
                async: false,
                success: function (data) {
                    if (data.length > 0) {
                        $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').val(data[0].PARAMETER_ROYALTY);
                        $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').attr("chan", "T");
                        $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').next().val(data[0].PARAMETER_ROYALTY);
                        banshui = data[0].PARAMETER_ROYALTY;//获取版税参数
                        var bsTrue = parseFloat(banshui); //版税的确认
                        var jshuTrue = parseFloat(NUMBER_DELIVERY);//本期的净发货数的确认
                        var priceTrue = Math.round(parseFloat(djPrice) * 100) / 100;//定价的确认
                        var money = bsTrue * jshuTrue * priceTrue;//应付价格
                        //判断是否促销:1 是；0否
                        if (ROYALTY_SALE == '0') {
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').val(Math.round(money * 100) / 100);
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').attr("chan", "T");
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').next().val(Math.round(money * 100) / 100);

                            var reallyMoney = ((money * 88.8) / 100); //实付
                            if (TYPE_TICKET == 'dz_fptype.003') {
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(reallyMoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(reallyMoney * 100) / 100);
                            } else {
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(money * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(money * 100) / 100);
                            }

                        } else {
                            var cx = parseFloat(NUMBER_SALE);//本期促销数的确认
                            var cxmoney = cx * priceTrue * 0.5 * bsTrue;//促销的金额
                            var yfmoney = money - cxmoney;//促销应付金额
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').val(Math.round(yfmoney * 100) / 100);
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').attr("chan", "T");
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').next().val(Math.round(yfmoney * 100) / 100);

                            var reallyMoney = ((yfmoney * 88.8) / 100); //实付
                            if (TYPE_TICKET == 'dz_fptype.003') {
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(reallyMoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(reallyMoney * 100) / 100);
                            } else {
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(yfmoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(yfmoney * 100) / 100);
                            }

                        }

                    }
                    //判断是否促销:1 是；0否
                    if (ROYALTY_SALE == '0') {
                        var str = "计算公式：" + jshuTrue + " * " + priceTrue + " * " + bsTrue + " = " + Math.round(money * 100) / 100;
                        //计算方式
                        $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').val(str);
                        $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').attr("chan", "T");
                        $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').next().val(str);

                    } else {
                        var str = "计算公式：" + jshuTrue + " * " + priceTrue + " * " + bsTrue + "  - " + cx + " * " + priceTrue + " * "
                            + bsTrue + " * " + 0.5 + "= " + Math.round(yfmoney * 100) / 100;
                        //计算方式
                        $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').val(str);
                        $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').attr("chan", "T");
                        $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').next().val(str);
                    }
                }
            });

        } //阶梯版税率
        else if (DJ_TYPE == 'contract.002') {
            $.ajax({
                type: 'post',
                url: _m_url + 'bjTopic/getZZBSMoney',
                data: {'xtId': XT_NAME, 'manID': PAY_PARTY},
                async: false,
                success: function (data) {
                    $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').val(data[0].PARAMETER_ROYALTY);
                    $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').attr("chan", "T");
                    $('#zzdz_addWinTable input[qucln=PARAMETER_ROYALTY]').next().val(data[0].PARAMETER_ROYALTY);
                    var runIds = new Array();
                    //获取进发货数
                    var rkMount = NUMBER_DELIVERY;
                    //获取版税临时表中的dsid
                    var dsId = 'ds_zc_authorcheck1';
                    var sinid = data[0].SIN_ID;
                    var price = djPrice;
                    runIds.push(rkMount);
                    runIds.push(dsId);
                    runIds.push(sinid);
                    runIds.push(PAY_PARTY);
                    runIds.push(XT_NAME);
                    runIds.push(price);
                    runIdtwo = runIds;
                    console.log(runIds);
                    $.ajax({
                        type: 'post',
                        url: _m_url + 'bjTopic/getzzJTBMTWO',
                        contentType: "application/json;charset=utf-8",
                        data: JSON.stringify(runIds),
                        async: false,
                        success: function (data) {
                            var rts = data[0].split("^");
                            var expStr = rts[0];
                            var bsmoney = rts[1];
                            var jttax = rts[3];

                            var reallyMoney = ((bsmoney * 88.8) / 100); //实付
                            if (TYPE_TICKET == 'dz_fptype.003') {
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(reallyMoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(reallyMoney * 100) / 100);
                            } else {
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(bsmoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(bsmoney * 100) / 100);
                            }
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').val(Math.round(bsmoney * 100) / 100);
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').attr("chan", "T");
                            $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').next().val(Math.round(bsmoney * 100) / 100);

                            //计算方式
                            $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').val(expStr);
                            $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').attr("chan", "T");
                            $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').next().val(expStr);

                            //判断是否促销:1 是；0否
                            if (ROYALTY_SALE == '0') {
                                $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').val(Math.round(bsmoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').next().val(Math.round(bsmoney * 100) / 100);

                                var reallyMoney = ((bsmoney * 88.8) / 100); //实付
                                if (TYPE_TICKET == 'dz_fptype.003') {
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(reallyMoney * 100) / 100);
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(reallyMoney * 100) /
                                    100);
                                } else {
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(bsmoney * 100) / 100);
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(bsmoney * 100) / 100);
                                }
                            } else {
                                var cx = parseFloat(NUMBER_SALE);//促销数的确认
                                //var jttax=str.substring(29, str.indexOf("B"));
                                var cxmoney = cx * price * 0.5 * jttax;//促销的金额
                                var yfmoney = bsmoney - cxmoney;//促销应付金额
                                $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').val(Math.round(yfmoney * 100) / 100);
                                $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').attr("chan", "T");
                                $('#zzdz_addWinTable input[qucln=PAY_SHOULD]').next().val(Math.round(yfmoney * 100) / 100);

                                var reallyMoney = ((yfmoney * 88.8) / 100); //实付
                                if (TYPE_TICKET == 'dz_fptype.003') {
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(reallyMoney * 100) / 100);
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(reallyMoney * 100) /
                                    100);
                                } else {
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').val(Math.round(yfmoney * 100) / 100);
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').attr("chan", "T");
                                    $('#zzdz_addWinTable input[qucln=PAY_REAL]').next().val(Math.round(yfmoney * 100) / 100);
                                }

                            }
                            var str1 = expStr + "\n" + "应付：" + bsmoney + "-(" + cx + " * " + price + " * " + jttax + " * " + 0.5 + ")= " + Math.round(yfmoney * 100) / 100;

                            //判断是否促销:1 是；0否
                            if (ROYALTY_SALE == '0') {
                                //计算方式
                                $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').val(expStr);
                                $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').attr("chan", "T");
                                $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').next().val(expStr);
                            } else {
                                //计算方式
                                $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').val(str1);
                                $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').attr("chan", "T");
                                $('#zzdz_addWinTable textarea[qucln=FORMULAS_CALCULATE]').next().val(str1);
                            }
                        }
                    })
                }
            });

        }//固定金额
        else if (DJ_TYPE == 'contract.003') {
            $.messager.alert('提示', '版税类型为“固定金额”，不进行对账,请重新选择对账的选题！');
            return;
        }//按千字
        else if (DJ_TYPE == 'contract.008') {
            $.messager.alert('提示', '版税类型为“按千字”，不进行对账,请重新选择对账的选题！');
            return;
        }
    }
    var cwzzId = $("#zzdz_addWinForm input[qucln='CWZZ_ID']").val();
    if (!cwzzId) {
        $(obj).attr('sou', 1);
    } else {
        $(obj).attr('sou', 2);
    }
    //验证明细对账月份是否大于等于明细最大月份
    var allRows =$('#'+$(obj).attr('pre')+"_addItemDatagrid").datagrid('getRows');
    var maxDate = new Date();
    $.each(allRows, function (i, v) {
        var month = new Date(v.ZZDZ_MONTH);
        if(i == 0) {
            maxDate = month;
        } else if(maxDate < month) {
            maxDate = month;
        }
    });
    if(maxDate > new Date(DZ_DATE)) {
        $.messager.alert("提示信息", "对账日期不能小于明细最大月份!");
        return;
    }
    common_save(obj);
}

//判断作者对账中是否存在以保存的付款对象和选题，如果有就提示并且限制
function is_existdzdata() {
    // 获取选题名称
    var CWZZ_ID = $('#zzdz_addWinTable').find('input[qucln=CWZZ_ID]').val();
    //获取对账单CWZZ_ID
    var XT_NAME = $('#zzdz_addWinTable').find('input[qucln=TOPIC_SELECTED]').val();
    // 获取付款对象
    var PAY_PARTY = $('#zzdz_addWinTable').find('input[qucln=PAY_PARTY]').val();
    // 获取结算依据
    var jiesuan = $('#zzdz_addWinTable').find('input[qucln=BASIS_ACCOUNT]').val();
    var   exist;
    $.ajax({
        type: 'post',
        url: _m_url + 'bjTopic/get_zzdata',
        data: {'CWZZ_ID': CWZZ_ID,'PAY_PARTY': PAY_PARTY, 'BASIS_ACCOUNT': jiesuan, 'TOPIC_SELECTED': XT_NAME},
        async: false,
        success: function (data) {
            if (data && data.length > 0) {
                $.messager.alert('提示', '已存在付款对象是：'+data[0].M_NAME+'，选题为《'+data[0].XT_NAME+'》保存且未审核的单据，请先审核掉上一单，再录新单，否则阶梯算法会不准！')
                exist= 1;
            }}})
    return exist;
}